import log from "./logger";
import settings from "./settings";
import controller from "./controller";
import weighter from "./weight";
import player from "./player";
import plate from "./plate";
import rtsp from "./rtsp";
import led from "./led";
// import aliyunIot from "./aliyuniot";
import state from "./states/state";
import container from "./container";
const {axios} = require('../node-test');
import metric from "./metric";
import printer from "./printer";
import qr from "./qr";
import webserver from "./webserver";
import mqttIot from "./mqttiot";
import promailSpawn from "./promailspawn";
const readline = require("readline");

async function start() {
    axios.defaults.timeout = 15 * 1000
    // 加载配置
    await settings.init();
    // 开守护进程
    promailSpawn.init();
    // 启动打印机
    printer.init();
    // 启动预览
    // 开启ws服务提供给浏览器用
    // 启动集装箱号识别
    container.init();
    // 启动控制器
    controller.init();
    // 开启重量端口
    weighter.init();
    // 初始化语音
    player.init();
    // 开启车号识别http服务器
    plate.init();
    // 开启二维码识别
    qr.init();
    // 初始化上传组件
    rtsp.init();
    // 初始化led
    led.init();
    // 初始化mqtt服务
    mqttIot.init();
    // webserver
    webserver.init();
    // 开启告警检测
    metric.init(60 * 1000);
    // 启动过磅状态机
    state.init();
    log.info("启动完成")
}

function test() {
    // 创建readline接口实例
    let r1 = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    })
    r1.setPrompt("Test> ");
    r1.prompt();
    r1.on("line", function (line: string) {
        line.trim();
        console.log("line:", line);
        if (line.startsWith("weight")) {
            let split = line.split(" ");
            let weight = parseFloat(split[1]);
            let wen = "1" === split[2];
            weighter.emit('weight_data', weight, wen);
        } else if (line.startsWith("plate")) {
            let split = line.split(" ");
            let car_num = split[1];
            let inout = split[2];
            plate.emit("plate", car_num, inout);
        } else if (line.startsWith("active")) {
            let split = line.split(" ");
            let R1 = "1" === split[1];
            let R2 = "1" === split[2];
            let R3 = "1" === split[3];
            let R4 = "1" === split[4];
            controller.emit("active", [R1, R2, R3, R4]);
        } else {
            console.log("没有找到命令！");
        }
        r1.prompt();
    });
}

start()
// test()
